﻿using AirlineBookingSystem.Bookings.Core.Entities;
using AirlineBookingSystem.Bookings.Core.Repositories;
using AirlineBookingSystem.BuildingBlocks.Contracts.EventBus.Messages;
using MassTransit;
using MediatR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AirlineBookingSystem.Bookings.Application.Commands
{
    public class CreateBookingHandler:IRequestHandler<CreateBookingCommand,Guid>
    {
        private readonly IBookingRepository _bookingRepository;
        private readonly IPublishEndpoint _publishEndpoint;

        public CreateBookingHandler(IBookingRepository bookingRepository,IPublishEndpoint publishEndpoint)
        {
            _bookingRepository = bookingRepository;
            _publishEndpoint = publishEndpoint;
        }
        public async Task<Guid> Handle(CreateBookingCommand request, CancellationToken cancellationToken)
        {
            var booking = new Booking 
            {
                Id=Guid.NewGuid(),
                FlightId=request.FlightId,
                PassengerName=request.PassengerName,
                SeatNumber = request.SeatNumber,
                BookingDate=DateTime.Now
            };

            await _bookingRepository.AddBookingAsync(booking);
            await _publishEndpoint.Publish(new FlightBookedEvent(
                booking.Id,
                booking.FlightId,
                booking.PassengerName,
                booking.SeatNumber,
                booking.BookingDate

            ));
            return booking.Id;
        }

        
    }
}
